﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Content;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.UI.Models.Shared;
using MethodWorx.CMS.Services;

namespace MethodWorx.CMS.UI.Controllers
{
    public class ContentTemplateController : Controller
    {
        private SchemaService service;
        //
        // GET: /ContentTemplate/
        public ContentTemplateController(SchemaService service)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            this.service = service;
        }

        [HttpGet]
        public ActionResult Create(Guid id)
        {
            var model = new CreateContentTemplateModel(this.service, id);
            if (model.PageTemplate == null)
                return RedirectToAction("Index", "Schema");

            return View(model);
        }

        [HttpPost]
        public ActionResult Add(Guid id, string submit, string childChoice,
            Guid? existingContentTemplate,
            [Bind(Prefix = "ConstraintDetails")]ConstraintDetails constraintDetails)
        {
            var model = new CreateContentTemplateModel(this.service, id);

            if (model.PageTemplate == null)
                return RedirectToAction("Refresh", "Schema");
            else
            {
                if (ModelState.IsValid && existingContentTemplate.HasValue)
                {
                    //  get the child
                    var child = this.service.GetContentTemplateById(existingContentTemplate.Value);
                    if (child != null)
                    {
                        //
                        //  ok, we have the child
                        try
                        {
                            //
                            //  ok, we are creating a child here
                            //  so lets use the service
                            this.service.AddChildContentTemplateOf(model.PageTemplate,
                                child,
                                constraintDetails.MinimumRequired,
                                constraintDetails.MaximumAllowed,
                                constraintDetails.NameConstraint);

                            return RedirectToAction("Refresh", "Schema");
                        }
                        catch (BusinessRuleException bre)
                        {
                            foreach (var error in bre.Errors)
                            {
                                //  add the model error here
                                this.ModelState.AddModelError(error.Key, error.Message);
                            }
                        }
                    }
                }

                return View("Create", model);
            }
        }

        [HttpPost]
        public ActionResult Create(Guid id, string submit, string childChoice,
            [Bind(Prefix = "ContentTemplateDetails")]ContentTemplateBasicDetails contentTemplateDetails,
            [Bind(Prefix = "ConstraintDetails")]ConstraintDetails constraintDetails)
        {
            //
            //  ok, lets read the parameters in
            var model = new CreateContentTemplateModel(this.service, id);

            //  no page template, so
            if (model.PageTemplate == null)
                return RedirectToAction("Refresh", "Schema");
            else
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        //
                        //  ok, we are creating a child here
                        //  so lets use the service
                        this.service.ChildChildContentTemplateOf(model.PageTemplate,
                            contentTemplateDetails.Name,
                            contentTemplateDetails.Description,
                            constraintDetails.MinimumRequired,
                            constraintDetails.MaximumAllowed,
                            constraintDetails.NameConstraint);

                        return RedirectToAction("Refresh", "Schema");
                    }
                    catch (BusinessRuleException bre)
                    {
                        foreach (var error in bre.Errors)
                        {
                            //  add the model error here
                            this.ModelState.AddModelError(error.Key, error.Message);
                        }
                    }


                }

                return View(model);
            }
        }



    }
}
